package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.annotation.TingShuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;


	//Request URL: http://localhost:8500/api/album/trackInfo/uploadTrack
	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file) {
		 Map<String,Object> map=trackInfoService.uploadTrack(file);
		return Result.ok(map);
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/saveTrackInfo
	@Operation(summary = "保存声音信息")
	@PostMapping("/saveTrackInfo")
	@TingShuLogin
	public Result saveTrack(@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/findUserTrackPage/1/1

	@Operation(summary = "分页查询用户声音信息")
	@PostMapping("/findUserTrackPage/{pn}/{pz}")
	@TingShuLogin
	public Result findUserTrackPage(@PathVariable("pn")  Integer pn,
									@PathVariable("pz") Integer pz,
									@RequestBody TrackInfoQuery trackInfoQuery){

		Long userId = AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		IPage<TrackListVo> trackListVoIPage = new Page<TrackListVo>(pn, pz);
		trackListVoIPage=trackInfoService.findUserTrackPage(trackListVoIPage,trackInfoQuery);
		return Result.ok(trackListVoIPage);
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/getTrackInfo/51943
	@Operation(summary = "声音数据回显")
	@GetMapping("/getTrackInfo/{trackId}")
	@TingShuLogin
	public Result getTrackInfo(@PathVariable("trackId") Long trackId){

		TrackInfo trackInfo = trackInfoService.getById(trackId);
		return Result.ok(trackInfo);
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/updateTrackInfo/51943

	@Operation(summary = "修改声音信息")
	@PutMapping("/updateTrackInfo/{trackId}")
	@TingShuLogin
	public Result updateTrackInfo(@PathVariable("trackId") Long trackId,
								  @RequestBody TrackInfoVo trackInfoVo){
		Long userId = AuthContextHolder.getUserId();
		trackInfoService.updateTrackInfo(trackInfoVo,trackId,userId);
		return Result.ok();
	}
	//Request URL: http://localhost:8500/api/album/trackInfo/removeTrackInfo/51943
	@Operation(summary = "删除声音信息")
	@DeleteMapping("/removeTrackInfo/{trackId}")
	@TingShuLogin
	public Result removeTrackInfo(@PathVariable("trackId") Long trackId){
		Long userId = AuthContextHolder.getUserId();
		trackInfoService.removeTrackInfo(userId,trackId);
		return Result.ok();
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/findAlbumTrackPage/1413/1/10


	/*
	* 这个接口是否要登录
	* 这个接口不一定需要你登录
	* 1)访问这个接口没有登录信息(没有携带token 不登录 直接访问这个接口是没有问题的
	* 2)访问了这个接口有登录信息(携带token) 在访问这个接口之前在别的地方登录了,需要获取得到token,携带得到的token
	* */
	@TingShuLogin(required = false)
	@Operation(summary = "分页查询专辑声音的声音列表")
	@GetMapping("/findAlbumTrackPage/{albumId}/{pn}/{pz}")
	public Result findAlbumTrackPage(@PathVariable("albumId") Long albumId,
									  @PathVariable("pn") Integer pn,
									  @PathVariable("pz") Integer pz){
		//1.分页对象的构建(对返回结果进行分页)
		IPage<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pn, pz);
		albumTrackListVoPage=trackInfoService.findAlbumTrackPage(albumTrackListVoPage,albumId);
		return Result.ok(albumTrackListVoPage);
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/getTrackStatVo/51946
	@Operation(summary = "查询声音的统计信息")
	@GetMapping("/getTrackStatVo/{trackId}")
	public Result getTrackStatVo(@PathVariable("trackId") Long trackId){
		 TrackStatVo trackStatVo= trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}

	//Request URL: http://localhost:8500/api/album/trackInfo/findUserTrackPaidList/20758

	@Operation(summary = "分集展示用户要购买的声音的列表")
	@GetMapping("/findUserTrackPaidList/{trackId}")
	@TingShuLogin
	public Result findUserTrackPaidList(@PathVariable("trackId") Long trackId){

		List<Map<String,Object>> mapList= trackInfoService.findUserTrackPaidList(trackId);

		 return Result.ok(mapList);
	}


}

